<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Upload extends CrmController
{
    /**
     * uploadPicture
     */
    public function uploadPicture()
    {
        $menuname = "Upload Picture";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);

        // 模板输出
        return $this->fetch('uploadPicture');
    }


    public function uploadPictureData ()
    {
        $arrData = Loader::model('Upload')->uploadPictureData();
        echo json_encode($arrData);
    }


    /**
     * addPicture
     */
    public function addPicture()
    {
        $menuname = "Upload Picture";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);

        // 模板输出
        return $this->fetch('addPicture');
    }


    /**
     * editPicture
     */
    public function editPicture()
    {
        $menuname = "Upload Picture";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);

        $arrData = Loader::model('Upload')->getPictureInfo($_REQUEST['id']);
        $this->assign("arrData",$arrData);
        // 模板输出
        return $this->fetch('editPicture');
    }


    public function uploadImage ()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // print_r($file);exit;
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                // echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $result['src'] = $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                $result['save_name'] = $info->getFilename();
            } else {
                // 上传失败获取错误信息
                $result['error'] = $file->getError();
                $result['code'] = 500;
            }
            // print_r($result);exit;
        }
        echo json_encode($result);
    }


    public function insertPicture ()
    {
        $data = $_REQUEST;
        $data['createtime'] = date('Y-m-d H:i:s');
        unset($data['file']);
        Loader::model('Upload')->insertPicture($data);
    }


    public function updatePicture () 
    {
        $data = $_REQUEST;
        Loader::model('Upload')->updatePicture($data);
    }


    public function delPicture ()
    {
        Loader::model('Upload')->delPicture($_REQUEST['id']);
    }

}